package com.webinsky.site.persist.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.type.JdbcType;

import com.webinsky.site.persist.entity.SiteEntity;

public interface SiteEntityMapper {

	@Delete({
		"delete from `site` where id = #{id, jdbcType=INTEGER}"
	})
	int deleteById(int id);
	
	@Insert({
		"insert into `site` (name, url, description, del, create_at, update_at, language)",
		"values (#{name,jdbcType=VARCHAR}, #{url,jdbcType=VARCHAR}, #{description,jdbcType=VARCHAR}, #{del,jdbcType=INTEGER}, now(), now(), #{lang,jdbcType=VARCHAR})"
	})
	@SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="id", before=false, resultType=Integer.class)
	int insert(SiteEntity record);
	
	@Select({
		"select id, name, url, description, del, create_at, update_at, language",
		"from `site`",
		"where id = #{id, jdbcType=INTEGER}"
	})
	@Results({
	    @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
	    @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
	    @Result(column="url", property="url", jdbcType=JdbcType.VARCHAR),
	    @Result(column="description", property="description", jdbcType=JdbcType.VARCHAR),
	    @Result(column="create_at", property="createAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="update_at", property="updateAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="language", property="lang", jdbcType=JdbcType.VARCHAR)
	})
	SiteEntity selectById(@Param("id")int id);
	
	@Select({
		"select id, name, url, description, del, create_at, update_at, language",
		"from `site`"
	})
	@Results({
	    @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
	    @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
	    @Result(column="url", property="url", jdbcType=JdbcType.VARCHAR),
	    @Result(column="description", property="description", jdbcType=JdbcType.VARCHAR),
	    @Result(column="create_at", property="createAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="update_at", property="updateAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="language", property="lang", jdbcType=JdbcType.VARCHAR)
	})
	List<SiteEntity> selectAll();
	
	@Update({
		"update `site`",
		"set name = #{name, jdbcType=VARCHAR},",
		"url = #{url, jdbcType=VARCHAR},",
		"description = #{description, jdbcType=VARCHAR},",
		"del = #{del, jdbcType=INTEGER},",
		"update_at = now()",
		"where id = #{id, jdbcType=INTEGER}"
	})
	void update(SiteEntity site);
	
}
